package cn.genmer.test.security;

public class Solution {
    public static int singleNumber(int[] nums) {
        int ans = nums[0];
        if (nums.length > 1) {
            for (int i = 1; i < nums.length; i++) {
                //循环结束后ans 就等于数组中所有元素异或后的结果
                ans = ans ^ nums[i];
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] nums = {7,7, 2,3,3,5,5,5};
//        System.out.println(singleNumber(nums));
//        System.out.println(getRightOne(12));

        printOddTimesNum(nums);
    }

    public static int getRightOne(int a) {
        return (a & -a);
    }

    // 打印2个出现次数奇数次的数字
    public static void printOddTimesNum(int[] arr) {
        int eor = 0;
        for (int i = 0; i < arr.length; i++) {
            eor ^= arr[i];
        }
        int rightOne = getRightOne(eor);
        int a = 0;
        int b = 0;
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i] & rightOne) != 0) {
                a ^= arr[i];
            }
        }
        System.out.println(a);
        System.out.println(a ^ eor);
    }

}